package com.vasthu.gui;

import com.vasthu.api.domain.Category;
import com.vasthu.api.service.CategoryManagerService;
import com.vasthu.core.service.CategoryManagerServiceImpl;
import com.vasthu.gui.utility.SpringUtilities;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class VstCategoryDataForm extends JDialog {

    private JLabel caption;
    private JLabel lCat;
    private JTextField tCat;
    private JLabel lDesc;
    private JTextField tDesc;
    private JLabel lMeasuringUnit;
    private JTextField tMeasuringUnit;
    private JButton add;
    private JButton update;
    private JButton delete;
    private JButton cancel;
    private Component spacer;
    int mode;
    private int tableRow;
    private VstTableCallBack callback;
    private long currentCategoryId;
    private CategoryManagerService categoryManagerService = null;

    public VstCategoryDataForm(int mode, int tableRow, VstTableCallBack callback) {
        this.tableRow = tableRow;
        this.callback = callback;

        categoryManagerService = new CategoryManagerServiceImpl();

        caption = new JLabel("Category", JLabel.CENTER);
        caption.setFont(new Font("Monospaced 12", Font.BOLD, 16));
        lCat = new JLabel("Category Name : ", JLabel.RIGHT);
        lCat.setFont(new Font("Monospaced 12", Font.PLAIN, 12));
        tCat = new JTextField();
        tCat.setPreferredSize(new Dimension(Integer.MAX_VALUE, 25));
        tCat.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));

        lDesc = new JLabel("Description : ", JLabel.RIGHT);
        lDesc.setFont(new Font("Monospaced 12", Font.PLAIN, 12));
        tDesc = new JTextField();
        tDesc.setPreferredSize(new Dimension(Integer.MAX_VALUE, 25));
        tDesc.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
        
        lMeasuringUnit = new JLabel("Measuring Unit : ", JLabel.RIGHT);
        lMeasuringUnit.setFont(new Font("Monospaced 12", Font.PLAIN, 12));
        tMeasuringUnit = new JTextField();
        tMeasuringUnit.setPreferredSize(new Dimension(Integer.MAX_VALUE, 25));
        tMeasuringUnit.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));

        add = new JButton("ADD");
        add.setPreferredSize(new Dimension(100, 40));
        add.setMaximumSize(new Dimension(100, 40));
        update = new JButton("UPDATE");
        update.setPreferredSize(new Dimension(100, 40));
        update.setMaximumSize(new Dimension(100, 40));
        delete = new JButton("DELETE");
        delete.setPreferredSize(new Dimension(100, 40));
        delete.setMaximumSize(new Dimension(100, 40));
        cancel = new JButton("CANCEL");
        cancel.setPreferredSize(new Dimension(100, 40));
        cancel.setMaximumSize(new Dimension(100, 40));

        JPanel panel = new JPanel(new SpringLayout());

        JPanel topPanel = new JPanel(new FlowLayout());
        topPanel.add(caption);
        topPanel.setPreferredSize(new Dimension(Integer.MAX_VALUE, 25));
        topPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
        panel.add(topPanel);

        JPanel dataPanel = new JPanel(new SpringLayout());
        dataPanel.add(lCat);
        dataPanel.add(tCat);
        dataPanel.add(lDesc);
        dataPanel.add(tDesc);
        dataPanel.add(lMeasuringUnit);
        dataPanel.add(tMeasuringUnit);
        SpringUtilities.makeCompactGrid(dataPanel,
                3, 2, //rows, cols
                8, 8, //initialX, initialY
                0, 8);

        panel.add(dataPanel);
        panel.add(Box.createVerticalGlue());

        JPanel bPanel = new JPanel();
        bPanel.setLayout(new BoxLayout(bPanel, BoxLayout.X_AXIS));
        bPanel.setPreferredSize(new Dimension(Integer.MAX_VALUE, 50));
        bPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 50));
        bPanel.add(Box.createHorizontalGlue());
        bPanel.add(add);
        bPanel.add(update);
        spacer = Box.createRigidArea(new Dimension(10, 10));
        bPanel.add(spacer);
        bPanel.add(delete);
        bPanel.add(Box.createRigidArea(new Dimension(10, 10)));
        bPanel.add(cancel);
        panel.add(bPanel);

        SpringUtilities.makeCompactGrid(panel,
                4, 1, //rows, cols
                8, 8, //initialX, initialY
                8, 8);

        switch (mode) {
            case VstGuiConstants.CATEGORY_FORM_ADD: {
                add.setVisible(true);
                update.setVisible(false);
                delete.setVisible(false);
                spacer.setVisible(false);
                break;
            }
            case VstGuiConstants.CATEGORY_FORM_UPDATE: {
                add.setVisible(false);
                update.setVisible(true);
                delete.setVisible(true);
                spacer.setVisible(true);
                break;
            }
        }

        setModalityType(ModalityType.APPLICATION_MODAL);
        setMinimumSize(new Dimension(400, 220));
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screen.width - 400) / 2, (screen.height - 220) / 2);
        add(panel);
        setVisible(false);

        initActions();
    }

    private void initActions() {

        add.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Category cat = getCategory();
                categoryManagerService.insertCategory(cat);
            }
        });

        update.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });

        delete.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                categoryManagerService.deleteCategoryById(currentCategoryId);
                // DB row must be deleted before this line
                callback.rowDeleted(tableRow);
            }
        });

        cancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                close();
            }
        });
    }

    public Category getCategory() {
        Category cat = new Category();
        cat.setCategoryName(tCat.getText());
        cat.setCategoryDescription(tDesc.getText());
        cat.setMeasuringUnit(tMeasuringUnit.getText());
        return cat;
    }

    public void populate(Category cat) {
        if (cat == null) {
            return;
        }

        currentCategoryId = cat.getCategoryId();
        tCat.setText(cat.getCategoryName());
        tDesc.setText(cat.getCategoryDescription());
        tMeasuringUnit.setText(cat.getMeasuringUnit());
    }

    private void close() {
        dispose();
    }
}